package com.kingyea.mobilepolice.apimanage.log.service.impl;

import com.kingyea.mobilepolice.apimanage.log.bean.InvokLog;
import com.kingyea.mobilepolice.apimanage.log.dao.InvokLogMapper;
import com.kingyea.mobilepolice.apimanage.log.service.InvokLogService;
import com.kingyea.mobilepolice.apimanage.log.vo.serviceFrequencyMonitoringCondition;
import com.kingyea.mobilepolice.apimanage.log.vo.serviceFrequencyMonitoringVO;
import com.kingyea.mobilepolice.apimanage.systemsync.Send2Queue;
import com.kingyea.mobilepolice.apimanage.utils.DateUtils;
import com.kingyea.mobilepolice.apimanage.utils.page.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description InvokLog 实现类
 * @date Created in 2018-03-11 16:32:09
 */
@Service
@Transactional
public class InvokLogServiceImpl implements InvokLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(InvokLogServiceImpl.class);

    @Resource
    private InvokLogMapper invokLogMapper;

    @Value("${server.regId}")
    private String regId;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public InvokLog getById(Long id) throws Exception {
        return this.invokLogMapper.getById(id);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<InvokLog> list(InvokLog invokLog) throws Exception {
        List<InvokLog> list = invokLogMapper.list(invokLog);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Send2Queue
    @Override
    public int deleteById(Long id) throws Exception {
        invokLogMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param invokLog
     * @return
     */
    @Send2Queue
    @Override
    public InvokLog save(InvokLog invokLog) throws Exception {
        invokLog.setRegId(regId);
        invokLogMapper.save(invokLog);
        return invokLog;
    }

    @Async
    @Override
    public Future<InvokLog> asyncSave(InvokLog invokLog) throws Exception {
        try {
            return new AsyncResult<>(save(invokLog));
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage());
            return new AsyncResult<>(null);
        }

    }

    /**
     * 更新
     *
     * @param invokLog
     * @return
     */
    @Send2Queue
    @Override
    public InvokLog update(InvokLog invokLog) throws Exception {
        invokLogMapper.update(invokLog);
        return invokLog;
    }

    @Override
    public Object searchByCondition(Map<String, Object> map) throws Exception {
        PageInfo.startPage(map);
        return PageInfo.getPageInfo(invokLogMapper.searchByCondition(map)
        );
    }

    /**
     * 带条件统计调用日志数量
     *
     * @return
     */
    @Override
    public Integer countByCondition(Map<String, Object> map) throws Exception {
        DateUtils.formatStartEndParamNoTime(map);
        return invokLogMapper.countByCondition(map);
    }

    @Override
    public List<serviceFrequencyMonitoringVO> getServiceFrequencyMonitoring(serviceFrequencyMonitoringCondition condition) throws Exception {
        return invokLogMapper.getServiceFrequencyMonitoring(condition);
    }

    @Override
    public Object searchServiceQualityMonitoring(Map<String, Object> map) throws Exception {
        PageInfo.startPage(map);
        return PageInfo.getPageInfo(invokLogMapper.searchServiceQualityMonitoring(map));
    }


}
